package top.yongyan0624.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Result;
import top.yongyan0624.entity.PropertyComplaint;
import top.yongyan0624.entity.PropertyPay;

import java.io.IOException;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yongyan
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/property/complaint")
public class PropertyComplaintController extends BaseController {
    @GetMapping("/list")
    public Result list(String title) {
        Page<PropertyComplaint> pageData = propertyComplaintService.page(getPage(), new QueryWrapper<PropertyComplaint>()
                .like(StrUtil.isNotBlank(title), "title", title).orderByDesc("create_time"));

        pageData.getRecords().forEach(g -> {
            g.setUser(sysUserService.getById(g.getUserId()));
            g.setImgList(StrUtil.split(g.getImg(), ',', true, true));
        });

        return Result.success(pageData);
    }

    @GetMapping("/info/{id}")
    public Result list(@PathVariable("id") Long id) {
        PropertyComplaint propertyComplaint = propertyComplaintService.getById(id);

        propertyComplaint.setUser(sysUserService.getById(propertyComplaint.getUserId()));
        propertyComplaint.setImgList(StrUtil.split(propertyComplaint.getImg(), ',', true, true));

        return Result.success(propertyComplaint);
    }

    @PostMapping("/save")
    public Result save(@RequestPart PropertyComplaint propertyComplaint, @RequestPart MultipartFile[] imgFiles) {
        try {
            if (imgFiles.length <= 0) {
                return Result.fail("文件为空");
            }
            //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
            String fileName = handleFile("propertyComplaint", propertyComplaint.getUserId(), imgFiles);

            //设置img虚拟路径
            propertyComplaint.setImg(fileName);

        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        propertyComplaintService.save(propertyComplaint);

        propertyComplaint.setUser(sysUserService.getById(propertyComplaint.getUserId()));
        propertyComplaint.setImgList(StrUtil.split(propertyComplaint.getImg(), ',', true, true));

        return Result.success(propertyComplaint);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('complaint:update')")
    public Result update(@RequestPart PropertyComplaint propertyComplaint, @RequestPart MultipartFile[] imgFiles) {
        try {
            if (imgFiles.length > 0) {
                //文件不为空，处理文件，存储文件并生成随机虚拟路径文件名
                String fileName = handleFile("propertyComplaint", propertyComplaint.getUserId(), imgFiles);

                //设置img虚拟路径
                propertyComplaint.setImg(fileName);
            }

        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return Result.fail("图片上传失败！");
        }

        propertyComplaintService.updateById(propertyComplaint);

        propertyComplaint.setUser(sysUserService.getById(propertyComplaint.getUserId()));
        propertyComplaint.setImgList(StrUtil.split(propertyComplaint.getImg(), ',', true, true));

        return Result.success(propertyComplaint);
    }

    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('complaint:delete')")
    public Result delete(@RequestBody Long[] ids) {
        propertyComplaintService.removeByIds(Arrays.asList(ids));

        return Result.success("");
    }
}
